package com.cenluan.gallery;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

/**
 * 相册
 * 
 * @author peak
 * 
 */
public class Gallery extends Model<Gallery> {

	private static final long serialVersionUID = 1L;

	public static final Gallery dao = new Gallery();

	public static final int STATUS_DISABLED = 0;
	public static final int STATUS_ACTIVATED = 1;

	/**
	 * 获取照片数量
	 * 
	 * @return
	 */
	public long getPhotoCount() {
		return Db.queryLong("select count(*) from t_photo where galleryId=?",
				getStr("id"));
	}

	/**
	 * 获取最后更新时间，即最后上传照片的时间
	 * 
	 * @return
	 */
	public Date getLastUpdateTime() {
		return Db.queryDate(
				"select max(createTime) from t_photo where galleryId=?",
				getStr("id"));
	}

	/**
	 * 分页查询，暂时先简单实现，后面会有复杂查询，到时候开启缓存
	 * 
	 * @param pn
	 * @param pz
	 * @return
	 */
	public Page<Gallery> page(int pn, int pz, String userId) {
		StringBuilder sql = new StringBuilder();
		List<Object> paras = new ArrayList<Object>();
		sql.append(" from t_gallery where 1=1 ");
		if (StrKit.notBlank(userId)) {
			sql.append(" and createrId=? ");
			paras.add(userId);
		}
		sql.append(" order by createTime desc ");
		return paginate(pn, pz, "select *", sql.toString(), paras.toArray());
	}

}
